package com.edu.hre.db.sql.pim;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreEmpLanguage;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.pim.EmpLanguageForm;
import com.edu.hre.struts.form.pim.EmpLanguageListForm;

public class EmpLanguageDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "empLanguageDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<EmpLanguageForm> getEmpLanguageList(int limit, int offset, int empNumber){

		ArrayList<EmpLanguageForm> listResult = new ArrayList<EmpLanguageForm>();

		String sql = "SELECT elang.id AS col1," +
					" lang.name AS col2," +
					" elang.read_competency AS col3," +
					" elang.speak_competency AS col4," +
					" elang.listen_competency AS col5," +
					" elang.write_competency AS col6" +

					" FROM hre_emp_language AS elang" +
					" LEFT OUTER JOIN hre_language AS lang ON elang.lang_id = lang.id" +
					" LEFT OUTER JOIN hre_employee AS emp ON elang.emp_number = emp.emp_number " +

					" WHERE emp.is_deleted = 0 " +
					" AND emp.emp_number = ? " +
					" ORDER BY elang.date_created DESC";

		List <Object[]> listObject = baseService.listArrayObject(sql, limit, offset, empNumber);
		for(int i = 0; listObject != null && i < listObject.size(); i++){

			try {

				Object[] objs = listObject.get(i);
				EmpLanguageForm language = new EmpLanguageForm();
	
				language.setId(Integer.parseInt(CommonUtils.normalizeObjectToString(objs[1], "0")));
				language.setEmpNumber(empNumber);
				language.setLanguage(CommonUtils.normalizeObjectToString(objs[2], ""));
	
				int read = Integer.parseInt(CommonUtils.normalizeObjectToString(objs[3], "0"));
				int speak = Integer.parseInt(CommonUtils.normalizeObjectToString(objs[4], "0"));
				int listen = Integer.parseInt(CommonUtils.normalizeObjectToString(objs[5], "0"));
				int write = Integer.parseInt(CommonUtils.normalizeObjectToString(objs[6], "0"));
				
				if (read < 0) {
					read = 0;
				}
				if (read > 10) {
					read = 10;
				}
				
				if (speak < 0) {
					speak = 0;
				}
				if (speak > 10) {
					speak = 10;
				}
				
				if (listen < 0) {
					listen = 0;
				}
				if (listen > 10) {
					listen = 10;
				}
				
				if (write < 0) {
					write = 0;
				}
				if (write > 10) {
					write = 10;
				}
				
				int total = (read + speak + write + listen) / 4;
				
				language.setReadCompetency(read + " / 10 ["  + StringConstants.ALL_COMPETENCIES[read] + "]");
				language.setSpeakCompetency(speak + " / 10 ["  + StringConstants.ALL_COMPETENCIES[speak] + "]");
				language.setListenCompetency(listen + " / 10 ["  + StringConstants.ALL_COMPETENCIES[listen] + "]");
				language.setWriteCompetency(write + " / 10 ["  + StringConstants.ALL_COMPETENCIES[write] + "]");
				language.setGeneralCompetency(total + " / 10 ["  + StringConstants.ALL_COMPETENCIES[total] + "]");
	
				listResult.add(language);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}

		return listResult;
	}

	public int countEmpLanguages(int empNumber){

		int count = baseService.countByColumns(new String[]{"empNumber"}, new int[]{Compare.EQUAL}, new Integer[]{empNumber}, "id", HreEmpLanguage.class);
		return count < 0 ? 0 : count;
	}

	public void deleteLanguages(EmpLanguageListForm form){

		ArrayList<EmpLanguageForm> info = (ArrayList<EmpLanguageForm>) form.getAllLanguages();

		for (int i = 0; i < info.size(); i++) {
			EmpLanguageForm work = info.get(i);
			if (work.isSelected()) {
				baseService.deleteFirstPojo("id", Compare.EQUAL, (long) work.getId(), null, null, 0, HreEmpLanguage.class);
			}
		}
	}
}
